<?php

/**
 * This is the model class for table "tbuser".
 *
 * The followings are the available columns in table 'tbuser':
 * @property integer $id
 * @property integer $idgroup
 * @property string $username
 * @property string $password
 * @property string $salt
 * @property string $email
 * @property integer $trangthai
 * @property string $hoten
 * @property string $ngaysinh
 * @property string $diachi
 * @property string $dienthoai
 * @property string $icon
 *
 * The followings are the available model relations:
 * @property Tbcredit[] $tbcredits
 * @property Tbmuadichvu[] $tbmuadichvus
 * @property Tbgroupuser $idgroup0
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	
	public $confirm;
	public $oldpassword;
	public $verifyCode;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbuser';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		$message ="<span class='ui-icon ui-icon-alert'></span><span class='message'>". Yii::t('yii','Vui lòng nhập {attribute}.')."</span>";
		return array(
			array('idgroup, trangthai', 'numerical', 'integerOnly'=>true),
			array('username, email, hoten, icon', 'length', 'max'=>50),
			array('confirm, username, password, email,hoten,dienthoai,verifyCode', 'required', 'message'=> $message),
			array('password', 'length', 'max'=>100),
			array('salt', 'length', 'max'=>6),
			array('password','length','min'=>6,'message'=>'Mật khẩu ít nhất 6 ký tự'),
			array('confirm', 'compare', 'compareAttribute'=>'password'),
			array('diachi', 'length', 'max'=>200),
			array('dienthoai', 'length', 'max'=>20),
			array('ngaysinh', 'safe'),
			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
			array('id, idgroup, username, password, salt, email, trangthai, hoten, ngaysinh, diachi, dienthoai, icon', 'safe', 'on'=>'search'),
		);
	}

	public function activeCaptcha()
    {
        $code = Yii::app()->controller->registerAction('captcha')->verifyCode;
        if ($code != $this->verifyCode)
            $this->addError('verifyCode', 'Mã Captcha không hợp lệ');
    }
    
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tbcredit' => array(self::HAS_ONE, 'Credit', 'iduser'),
			'tbmuadichvus' => array(self::HAS_MANY, 'Tbmuadichvu', 'iduser'),
			'tbgroup' => array(self::BELONGS_TO, 'Groupuser', 'idgroup'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'idgroup' => 'Nhóm',
			'username' => 'Tên đăng nhập',
			'password' => 'Mật khẩu',
			'salt' => 'Salt',
			'email' => 'Email',
			'trangthai' => 'Trạng thái',
			'hoten' => 'Họ tên',
			'ngaysinh' => 'Ngày sinh',
			'diachi' => 'Địa chỉ',
			'dienthoai' => 'Điện thoại',
			'icon' => 'Photo',
			'verifyCode' => 'Mã bảo vệ',
			'confirm'=>'Nhập lại mật khẩu'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('idgroup',$this->idgroup);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('salt',$this->salt,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('trangthai',$this->trangthai);
		$criteria->compare('hoten',$this->hoten,true);
		$criteria->compare('ngaysinh',$this->ngaysinh,true);
		$criteria->compare('diachi',$this->diachi,true);
		$criteria->compare('dienthoai',$this->dienthoai,true);
		$criteria->compare('icon',$this->icon,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>100),
		));
	}
}